#!./common-test/bats/bin/bats
# -*-sh-*-

load ./common-test/test_helper_functions || exit 1
source ./common-test/functions || exit 1

if [ -s ./common-test/TEST_ENV ];then
   . ./common-test/TEST_ENV
else
    ERROR "TEST_ENV does not exist"
fi

CMD_TIMEOUT="5:00"
TEST_MAX_COMPUTES=1024
TEST_NUM_RANKS=2
TEST_NUM_THREADS=4
TEST_NUM_ITERS=10

app="App/PRK"
mode=OPENMP
rm=$RESOURCE_MANAGER
NUMITERS=$TEST_NUM_ITERS
NUMTHREADS=$TEST_NUM_THREADS

@test "[$app] $mode/Branch run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Branch/branch"
    for type in vector_go vector_stop no_vector ins_heavy; do
	run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 1000 $type
	assert_success
    done
}

@test "[$app] $mode/DGEMM run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/DGEMM/dgemm"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 500 32
    assert_success
}

@test "[$app] $mode/Nstream run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Nstream/nstream"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 2000000 0
    assert_success
}

@test "[$app] $mode/Random run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Random/random"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS 20 16 4
    assert_success
}

@test "[$app] $mode/Reduce run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Reduce/reduce"
    for ALGORITHM in linear binary-barrier binary-p2p long-optimal; do
	run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 2000000 $ALGORITHM
	assert_success
    done
}

@test "[$app] $mode/RefCount_shared run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/RefCount_shared/shared"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS 2000000
    assert_success
}

@test "[$app] $mode/RefCount_private run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/RefCount_private/private"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS 2000000
    assert_success
}

@test "[$app] $mode/Sparse run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Sparse/sparse"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 10 4
    assert_success
}

@test "[$app] $mode/Stencil run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Stencil/stencil"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 1000
    assert_success
}

@test "[$app] $mode/Synch_global run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Synch_global/global"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 1000
    assert_success
}

@test "[$app] $mode/Synch_p2p run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Synch_p2p/p2p"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 1000 100
    assert_success
}

@test "[$app] $mode/Transpose run under resource manager ($rm/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    exe="$mode/Transpose/transpose"
    run_serial_binary -t $CMD_TIMEOUT $exe $NUMTHREADS $NUMITERS 2000 64
    assert_success
}
